/// @tags:
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>
#include <string>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

constexpr int maxn = 5e5;

std::string a[maxn];

struct Splay {
  int n, tot, rt;
  int ans[maxn + 5];
  struct Node {
    int fa, size, flag, cnt, val, son[2];
  } s[maxn + 5];

  inline bool id(int x) { return s[s[x].fa].son[1] == x; }

  inline void update(int x) {
    if (x) {
      s[x].size = s[x].cnt;
      if (s[x].son[0]) s[x].size += s[s[x].son[0]].size;
      if (s[x].son[1]) s[x].size += s[s[x].son[1]].size;
    }
  }

  inline void connect(int x, int fa, bool opt) {
    if (x) s[x].fa = fa;
    if (fa) s[fa].son[opt] = x;
  }

  inline void rotate(int x) {
    int fa = s[x].fa, grfa = s[fa].fa;
    bool xtofa = id(x), fatogrfa = id(fa);
    connect(s[x].son[1 ^ xtofa], fa, xtofa);
    connect(fa, x, 1 ^ xtofa);
    connect(x, grfa, fatogrfa);
    update(fa);
    update(x);
  }

  inline void splay(int x, int y) {
    int cur = s[x].fa;
    while (cur != y) {
      if (s[cur].fa != y) rotate(id(x) == id(cur) ? cur : x);
      rotate(x);
      cur = s[x].fa;
    }
    if (y == 0) rt = x;
  }

  inline void pushdown(int x) {
    if (x && s[x].flag) {
      s[s[x].son[0]].flag ^= 1;
      s[s[x].son[1]].flag ^= 1;
      std::swap(s[x].son[0], s[x].son[1]);
      s[x].flag = 0;
    }
  }

  int build(int l, int r, int fa) {
    if (l > r) return 0;
    int mid = (l + r) >> 1;
    int cur = ++tot;
    s[cur].fa = fa;
    ++s[cur].cnt;
    s[cur].val = mid;
    if (s[cur].val == 1) s[cur].val = -0x7fffffff;
    if (s[cur].val == n + 2) s[cur].val = 0x7fffffff;
    s[cur].son[0] = build(l, mid - 1, cur);
    s[cur].son[1] = build(mid + 1, r, cur);
    update(cur);
    return cur;
  }

  inline int find(int x) {
    int cur = rt;
    while (true) {
      pushdown(cur);
      if (x <= s[s[cur].son[0]].size)
        cur = s[cur].son[0];
      else {
        x -= s[s[cur].son[0]].size + 1;
        if (!x) return cur;
        cur = s[cur].son[1];
      }
    }
  }

  inline void reverse(int x, int y) {
    int l = x - 1, r = y + 1;
    l = find(l), r = find(r);
    splay(l, 0);
    splay(r, l);
    s[s[s[rt].son[1]].son[0]].flag ^= 1;
  }

  int cnt = 0;

  inline void forea(int cur) {
    pushdown(cur);
    if (s[cur].son[0]) forea(s[cur].son[0]);
    if (s[cur].val != 0x7fffffff && s[cur].val != -0x7fffffff)
      ans[++cnt] = s[cur].val;
    if (s[cur].son[1]) forea(s[cur].son[1]);
  }
} r, c;

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    a[i].reserve(m);
    cin >> a[i];
  }
  r.n = n, c.n = m;
  r.rt = r.build(1, n + 2, 0);
  c.rt = c.build(1, m + 2, 0);
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    r.reverse(2, a + 1), r.reverse(a + 2, n + 1);
    c.reverse(2, b + 1), c.reverse(b + 2, m + 1);
  }
  r.forea(r.rt), c.forea(c.rt);
  for (int i = 1; i <= n; ++i) {
    int row = r.ans[i] - 2;
    for (int j = 1; j <= m; ++j) {
      int col = c.ans[j] - 2;
      cout << a[row][col];
    }
    cout << '\n';
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("B - Grid Rotations.in", "r", stdin);
  freopen("B - Grid Rotations.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
